<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/20
 * Time: 03:20
 */

namespace App\Http\Controllers\V1;

use App\Http\Services\UserService;
use App\Http\Services\WorkService;
use Illuminate\Http\Request;

class StudentController extends UserController
{

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    public function workList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $page = $request->get('page');
            $pageSize = $request->get('page_size');
            $offset = ($page - 1) * $pageSize;
            $order = $request->get('order');
            $score = $request->get('score');
            $user = $this->getUser();
            $option = [
                'user_id' => $user['id'],
                'score' => $score
            ];
            $data = WorkService::getList($pageSize, $offset, $order, $option);
            $this->sendJson($data);
        });
    }

    public function saveWork(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $user = $this->getUser();
            $data = [
                'user_id' => $user['id'],
                'category_id' => $params['category'],
                'title' => $params['title'],
                'desc' => $params['desc'],
                'content' => trim($params['content'], ';')
            ];
            try {
                WorkService::save($data);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function updateWork(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $data = [
                'id' => $params['id'],
                'category_id' => $params['category'],
                'title' => $params['title'],
                'desc' => $params['desc'],
                'content' => trim($params['content'], ';')
            ];
            try {
                WorkService::save($data);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function deleteWork(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            try {
                WorkService::delete($params['id']);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function getFile(Request $request)
    {
        $this->sendGet(function() use($request) {
            $user = $this->getUser();
            $this->sendJson($user);
        });
    }

    public function updateFile(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $params['id'] = $this->jwtCode['uid'];
            try {
                UserService::save($params);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }

    public function changePass(Request $request)
    {
        $this->sendPost(function() use($request) {
            $params = $request->get('params');
            $data = [
                'id' => $this->jwtCode['uid'],
                'old_pass' => md5($params['old_pass']),
                'new_pass' => md5($params['new_pass']),
                'new_pass_sec' => md5($params['new_pass_sec'])
            ];
            try {
                UserService::changePass($data);
                $this->sendJson([]);
            } catch (\Exception $e) {
                $this->sendJson([], $e->getCode(), $e->getMessage());
            }
        });
    }
}